home *** CD-ROM | disk | FTP | other *** search
/ PC Active 2009 July/August / PC Active NR.227.iso / Software / Games / windows / Freeciv-2.1.9-win32-gtk2-setup.exe / doc / README.AI < prev    next >
Encoding:
Text File  |  2009-03-25  |  13.5 KB  |  341 lines

  1. ==============
  2. THE FREECIV AI
  3. ==============
  4.  
  5.  
  6. CONTENTS
  7. ========
  8. Introduction
  9. Contacting the current AI developers
  10. Long-term AI development goals
  11. Want calculations
  12. Amortize
  13. Estimation of profit from a military operation
  14. Selecting military units
  15. Ferry system
  16. Diplomacy
  17. Difficulty levels
  18. Things that needs to be fixed
  19. Idea space
  20.  
  21.  
  22. INTRODUCTION 
  23. ============ 
  24.  
  25. The Freeciv AI is widely recognized as being as good as or better
  26. military-wise as the AI of certain other games it is natural to compare
  27. it with.  It is, however, still too easy for experienced players.
  28.  
  29. The code base used to be in a bad shape but it has gotten a lot
  30. better.  The reason for this is that the developer (Syela) who in a
  31. few months put together a working AI had suddenly disappeared.  His
  32. bright ideas could only be matched by his inability to name variables
  33. and to comment the code.  Subsequent AI developers were not brave (or
  34. stupid?) enough to start from scratch, taking instead a small bite
  35. here and there, trying hard not to break much, to understand Syela's
  36. original design and only then to throw it away.  Or perfect it.
  37.  
  38. Not all code is residing in ai/, but it is also dissolved in little 
  39. chunks in the whole server/.  Aside that, server/settlers.c is only AI 
  40. stuff - the problem is, that most of it is used also for the 
  41. auto-settlers, so we can't separate it from the server.  
  42. server/gotohand.c is a left-over from the previous path-finding system 
  43. which is slowly being phased out.
  44.  
  45.  
  46.  
  47. CONTACTING THE CURRENT AI DEVELOPERS
  48. ====================================
  49.  
  50. AI development has its own mailing list. Send questions to
  51. freeciv-ai@freeciv.org, or go to 
  52.  
  53.   http://www.freeciv.org/index.php/Community_Forums
  54.  
  55. to read the archives or to join up.
  56.  
  57.  
  58. LONG-TERM AI DEVELOPMENT GOALS
  59. ==============================
  60.  
  61. The long-term goals for Freeciv AI development are
  62.  -> to create a challenging and fun AI for human players to defeat
  63.  -> to create an AI that can handle all the rules possibilities that
  64.     Freeciv can offer
  65.  
  66.  
  67. WANT CALCULATIONS
  68. =================
  69.  
  70. Build calculations are expressed through a structure called ai_choice. 
  71. This has a variable called "want", which determines how much the AI 
  72. wants whatever item is pointed to by choice->type. choice->want is
  73.  
  74.    -199   get_a_boat
  75.    < 0    an error
  76.    == 0   no want, nothing to do
  77.    <= 100 normal want
  78.     > 100 critical want, used to requisition emergency needs
  79.     > ??? probably an error (1024 is a reasonable upper bound)
  80.     > 200 Frequently used as a cap. When want exceeds this value,
  81.           it is reduced to a lower number.
  82.  
  83. These are ideal numbers, your mileage while travelling through the 
  84. code may vary considerably.  Technology and diplomats, in particular, 
  85. seem to violate these standards.
  86.  
  87.  
  88. AMORTIZE
  89. ========
  90.  
  91. Hard fact:
  92. amortize(benefit, delay) returns benefit * ((MORT - 1)/MORT)^delay
  93. (where "^" == "to the power of")
  94.  
  95. Speculation:
  96. What is better, to receive 10$ annually starting in 5 years from now
  97. or 5$ annually starting from this year?  How can you take inflation
  98. into account?  The function amortize is meant to help you answer these
  99. questions.  To achieve this, it rescales the future benefit in terms of
  100. todays money.
  101.  
  102. Suppose we have a constant rate of inflation, x percent.  Then in five
  103. years time 10$ will buy as much as 10*(100/(100+x))^5 will buy today.
  104. Denoting 100/(100+x) by q we get the general formula, N dollars Y
  105. years from now will be worth N*q^Y in todays money.  If we will
  106. receive N every year starting Y years from now, the total amount
  107. receivable (in todays money) is N*q^Y / (1-q) --- this is the sum of
  108. infinite geometric series.  This is exactly the operation that
  109. amortize performs, the multiplication by some q < 1 raised to power Y.
  110. Note that the factor 1/(1-q) does not depend on the parameters N and Y
  111. and can be ignored.  The connection between MORT constant and the
  112. inflation rate x is given by
  113.     (MORT - 1) / MORT = q = 100 / (100 + x).
  114. Thus the current value of MORT = 24 corresponds to the inflation rate
  115. (or the rate of expansion of your civ) of 4.3%
  116.  
  117. Most likely this explanation is not what the authors of amortize() had
  118. in mind, but the basic idea is correct: the value of the payoff decays
  119. exponentially with the delay.
  120.  
  121. The version of amortize used in the military code (military_amortize())
  122. remains a complete mystery.
  123.  
  124.  
  125. ESTIMATION OF PROFIT FROM A MILITARY OPERATION
  126. ==============================================
  127.  
  128. This estimation is implemented by kill_desire function (which isn't
  129. perfect: multi-victim part is flawed) plus some corrections.  In
  130. general,
  131.         Want = Operation_Profit * Amortization_Factor
  132. where 
  133.  
  134. * Amortization_Factor is completely beyond me (but it's a function of the
  135. estimated time length of the operation).
  136.  
  137. * Operation_Profit = Battle_Profit - Maintenance
  138.  
  139. where
  140.  
  141. * Maintenance 
  142.   = (Support + Unhappiness_Compensation) * Operation_Time 
  143.   (here unhappiness is from military unit being away from home
  144.    and Support is the number of shields spent on supporting this unit 
  145.    per turn )
  146.  
  147. * Battle_Profit
  148.   = Shields_Lost_By_Enemy * Probability_To_Win 
  149.     - Shields_Lost_By_Us * Probability_To_Lose
  150.  
  151. That is Battle_Profit is a probabilistic average.  It answer the
  152. question "how much better off, on average, we would be from attacking
  153. this enemy unit?"
  154.  
  155.  
  156. SELECTING MILITARY UNITS
  157. ========================
  158.  
  159. The code dealing with choosing military units to be built and targets
  160. for them is especially messy.  Here is what we've managed to decipher.
  161.  
  162. Military units are requested in military_advisor_choose_build
  163. function.  It first considers the defensive units and then ventures
  164. into selection of attackers (if home is safe).  There are 2
  165. possibilities here: we just build a new attacker or we already have an
  166. attacker which was forced, for some reason, to defend.  In the second
  167. case it's easy: we calculate how good the existing attacker is and if
  168. it's good, we build a defender to free it up.
  169.  
  170. Building a brand-new attacker is more complicated.  Firstly,
  171. ai_choose_attacker_* functions are charged to find the first
  172. approximation to the best attacker that can be built here.  This
  173. prototype attacker is selected using very simple attack_power * speed
  174. formula.  Then (already in kill_something_with) we search for targets
  175. for the prototype attacker (using find_something_to_kill).  Having
  176. found a target, we do the last refinement by calling
  177. process_attacker_want to look for the best attacker type to take out
  178. the target.  This type will be our attacker choice.  Note that the
  179. function process_attacker_want has side-effects wrt the tech selection. 
  180.  
  181. Here is an example:
  182.  
  183. First ai_choose_attacker_land selects a Dragoon because it's strong
  184. and fast.  Then find_something_to_kill finds a victim for the
  185. (virtual) Dragoon, an enemy Riflemen standing right next to the town.
  186. Then process_attacker_want figures out that since the enemy is right
  187. beside us, it can be taken out easier using an Artillery.  It also
  188. figures that a Howitzer would do this job even better, so bumps up our
  189. desire for Robotics.
  190.  
  191. This is the idea, anyway.  In practice, it is more complicated and
  192. probably less efficient.
  193.  
  194.  
  195. FERRY SYSTEM
  196. ============
  197.  
  198. The ferry (i.e. boats transporting land units) system of Freeciv is
  199. probably better described by statistical mechanics than by logic.
  200. Both ferries and prospective passenger (PP) move around in what looks
  201. like a random fashion, trying to get closer to each other.  On
  202. average, they succeed.  This behaviour has good reasons behind it, is
  203. hell to debug but means that small bugs don't affect overall picture
  204. visibly (and stay unfixed as a result).
  205.  
  206. Each turn both boats and PPs forget all about prior arrangements
  207. (unless the passenger is actually _in_ the boat).  Then each will look
  208. for the closest partner, exchange cards and head towards it.  This is
  209. done in a loop which goes through all units in essentially random
  210. order.
  211.  
  212. Because most units recalculate their destination every turn, ignoring
  213. prior arrangements is the only good strategy -- it means that a boat
  214. will not rely on the PP to notify it when it's not needed anymore.
  215. This is not very effective but can only be changed when the PPs behave
  216. more responsibly.  See diplomat code for more responsible behaviour --
  217. they try to check if the old target is still good before trying to
  218. find a new one.
  219.  
  220. When a boat has a passenger, it's a different story.  The boat doesn't
  221. do any calculations, instead one of the passengers is given full
  222. control and it is the passenger who drives the boat.
  223.  
  224. Here are the main data fields used by the system.
  225. Value of ai.ferry in the passenger unit is:
  226.   FERRY_NONE : means that the unit has no need of a ferry
  227.   FERRY_WANTED : means that the unit wants a ferry
  228.   >0 : id of it's ferry
  229. Value of ai.passenger in the ferry unit can be either of:
  230.   FERRY_AVAILABLE : means that the unit is a ferry and is available
  231.   >0 : id of it's passenger
  232.  
  233. When boat-building code stabilizes, it can be seen how many free boats
  234. there are, on average, per PP.  If there are more boats than PPs, it
  235. makes sense that only PPs should look for boats.  If boats are few,
  236. they should be the ones choosing.  This can be done both dynamically
  237. (both possibilities are coded and the appropriate is chosen every
  238. turn) and statically (after much testing only one system remains).
  239. Now they exist in parallel, although developed to a different degree.
  240.  
  241.  
  242. DIPLOMACY
  243. =========
  244.  
  245. The AI's diplomatic behaviour is current only regulated by the 
  246. 'diplomacy' server setting.
  247.  
  248. In default rules, the AI starts out in NO_CONTACT mode, and proceeds 
  249. to NEUTRAL on first-contact.
  250.  
  251. AI is not very trusting for NEUTRAL and PEACE modes, but once it hits 
  252. ALLIANCE, this changes completely, and it will happily hand over 
  253. any tech and maps it has to you.  The only thing that will make the AI 
  254. attack you then is if you build a spaceship.
  255.  
  256. For people who want to hack at this part of the AI code, please note
  257.  * pplayers_at_war(p1,p2) returns FALSE if p1==p2
  258.  * pplayers_non_attack(p1,p2) returns FALSE if p1==p2
  259.  * pplayers_allied(p1,p2) returns TRUE if p1==p2 
  260.  * pplayer_has_embassy(p1,p2) returns TRUE if p1==p2
  261. i.e. we do not ever consider a player to be at war with himself, we
  262. never consider a player to have any kind of non-attack treaty with
  263. himself, and we always consider a player to have an alliance with
  264. himself. 
  265.  
  266. The introduction of diplomacy is fraught with many problems.  One is
  267. that it usually gains only human players, not AI players, since humans
  268. are so much smarter and know how to exploit diplomacy, while for AIs
  269. they mostly only add constraints on what it can do.  Another is that it
  270. can be very difficult to write diplomacy that is useful for and not in
  271. the way of modpacks.  Which means diplomacy either has to be optional,
  272. or have fine-grained controls on who can do what diplomatic deals to
  273. whom, set from rulesets.  The latter is not yet well implemented.
  274.  
  275.  
  276. DIFFICULTY LEVELS
  277. =================
  278.  
  279. There are currently five difficulty levels: 'novice', 'easy', 'medium', 
  280. 'hard' and 'experimental'.  The 'hard' level is no-holds-barred, 
  281. while 'medium' has a number of handicaps.  In 'easy', the AI 
  282. also does random stupid things through the ai_fuzzy function. The
  283. 'experimental' level is only for coding - you can gate new code
  284. with the H_EXPERIMENTAL handicap and test 'experimental' level
  285. AIs against 'hard' level AIs. In 'novice' the AI researches slower
  286. than normal players.
  287.  
  288. Other handicaps used are:
  289.   H_RATES, can't set its rates beyond government limits
  290.   H_TARGETS, can't target anything it doesn't know exists
  291.   H_HUTS, doesn't know which unseen tiles have huts on them
  292.   H_FOG, can't see through fog of war
  293.  
  294. The other defined handicaps (in common/player.h) are not currently in 
  295. use.
  296.  
  297.  
  298. THINGS THAT NEED TO BE FIXED
  299. ============================
  300.  
  301. * Cities don't realize units are on their way to defend it.
  302. * AI doesn't understand that some wonders are obsolete, that some 
  303. wonders become obsolete, and doesn't upgrade units.
  304. * AI doesn't understand how to favor trade when it needs luxury.
  305. * AI builds cities without regard to danger at that location.
  306. * AI won't build cross-country roads outside of city radii.
  307. [Note: There is patch that permits the AI to build cross country
  308. roads/rail.  Unfortunately, it makes it too easy for the AI to be
  309. invaded. Addendum: Can someone actually test this? Defeating an AI 
  310. that uses roads is more fun, but is it really that much easier?]
  311. * Locally_zero_minimap is not implemented when wilderness tiles 
  312. change.
  313. * If no path to chosen victim is found, new victim should be chosen.
  314. * AI doesn't know how to make trade routes or when.  It should try to 
  315. build trade routes for its best cities (most building bonuses and 
  316. least corruption) by moving caravans there and changing homecity.
  317. * Emergencies in two cities at once aren't handled properly.
  318. * Explorers will not use ferryboats to get to new lands to explore.
  319. * AI sometimes believes that wasting a horde of weak military units to
  320. kill one enemy is profitable (PR#1340)
  321. * Stop building ships and shore defense in landlocked cities with a
  322. pond adjacent.
  323. * Make the AI building evaluation code use the new buildings.ruleset.
  324. * Fix the AI valuation of supermarket. (It currently never builds it).
  325. See farmland_food() and ai_eval_buildings() in advdomestic.c
  326. * Teach the AI to coordinate the units in an attack (ok, this one is a bit
  327. big...)
  328.  
  329.  
  330. IDEA SPACE
  331. ==========
  332.  
  333. * Friendly cities can be used as beachheads
  334. * Assess_danger should acknowledge positive feedback between multiple 
  335. attackers
  336. * It would be nice for bodyguard and charge to meet en-route more 
  337. elegantly.
  338. * struct choice should have a priority indicator in it.  This will
  339. reduce the number of "special" want values and remove the necessity to
  340. have want capped, thus reducing confusion.
  341.